package com.cd.university.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cd.university.common.RestResult;
import com.cd.university.controller.form.RolePermissionForm;
import com.cd.university.service.PermissionService;
import com.cd.university.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈栋
 * @create 2021/9/21 12:31
 */
@RestController
@RequestMapping("/rolePer")
@Api("操作权限接口")
public class RolePermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/allPermission")
    @ApiOperation("获得权限树")
    public RestResult getAllPermission() {
        return RestResult.success("获取所有的权限列表")
                .put("data", permissionService.searchAllPermissionVo());
    }

    @GetMapping("/rolePermission/{roleId}")
    @ApiOperation("根据角色查询角色的所有权限")
    @PreAuthorize("hasAnyAuthority('教师','超级管理员')")
    public RestResult searchPermissionByRoleId(@PathVariable Integer roleId) {
        try {
            List<PermissionVo> permissionLeafVos = permissionService.searchPermissionLeafByRoleId(roleId);
            List<PermissionVo> allPermissionVoList = permissionService.searchPermissionByRoleId(roleId);
            return RestResult.success("用户角色权限如下")
                    .put("LeafList", permissionLeafVos)
                    .put("AllList",allPermissionVoList);
        }catch (Exception e) {
            return RestResult.failure(e.getMessage());
        }
    }

    @PostMapping("/insertPermission")
    @ApiOperation("向角色中增加权限")
    @PreAuthorize("hasAnyAuthority('超级管理员','分配角色','管理角色')")
    public RestResult insertPermission(@RequestBody JSONObject jsonObject) {
        // 把前端传过来的list集合解析成JSONArray对象
        JSONArray array = jsonObject.getJSONArray("list");
        // SONArray对象转成Java的List集合
        List<RolePermissionForm> rolePermissionFormList = array.toJavaList(RolePermissionForm.class);

        try {
            permissionService.insertPermission(rolePermissionFormList);
            List<PermissionVo> permissionVos = permissionService.searchPermissionByRoleId(rolePermissionFormList.get(0).getRoleId());
            return RestResult.success("增加权限成功")
                    .put("data", permissionVos);
        }catch (Exception e) {
            e.printStackTrace();
            return RestResult.failure("权限添加失败");
        }
    }

    @PostMapping("/deletePermission")
    @ApiOperation("删除用户角色对应的权限")
    @PreAuthorize("hasAnyAuthority('超级管理员','分配角色','管理角色')")
    public RestResult deletePermission(@RequestBody JSONObject jsonObject) {
        // 把前端传过来的list集合解析成JSONArray对象
        JSONArray array = jsonObject.getJSONArray("list");
        // SONArray对象转成Java的List集合
        List<RolePermissionForm> rolePermissionFormList = array.toJavaList(RolePermissionForm.class);

        try {
            permissionService.deletePermissionId(rolePermissionFormList);
            List<PermissionVo> permissionVos = permissionService.searchPermissionByRoleId(rolePermissionFormList.get(0).getRoleId());
            return RestResult.success("删除权限成功")
                    .put("data", permissionVos);
        }catch (Exception e) {
            e.printStackTrace();
            return RestResult.failure("删除权限失败");
        }
    }

}
